package com.example.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dto.BoardroomStatusDTO;
import com.example.demo.pojo.Boardroom;
import com.example.demo.pojo.BoardroomState;
import com.example.demo.service.BoardroomService;
import com.example.demo.service.BoardroomStateService;
import com.example.demo.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjy
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/boardroom-state")
@Api(tags = "被选中的会议室状态管理")
public class BoardroomStateController {
    //Hailin Gong start
    @Autowired
    private BoardroomStateService boardroomStateService;
    @Autowired
    private BoardroomService boardroomService;

    @GetMapping("/getBoardroomStatusList")
    @ApiOperation("获取会议室状态列表（分页）")
    public ResultVO<IPage<BoardroomState>> getBoardroomStatusList(
            @RequestParam Integer boardroomId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @RequestParam(required = false) String startTime, // 修改为字符串
            @RequestParam(required = false) String endTime,   // 修改为字符串
            @RequestParam(required = false) Integer state,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<BoardroomState> page = new Page<>(current, pageSize);
        IPage<BoardroomState> result = boardroomStateService.getBoardroomStatusList(
                page, boardroomId, date, startTime, endTime, state
        );
        return ResultVO.success("查询成功！", result);
    }

    @PostMapping("/updateBoardroomStatus")
    @ApiOperation("更新会议室状态")
    public ResultVO<Boolean> updateBoardroomStatus(@RequestBody BoardroomState boardroomState) {
        boolean success = boardroomStateService.updateBoardroomStatus(boardroomState);
        if (success) {
            return ResultVO.success("更新成功！", true);
        } else {
            return ResultVO.fail( "更新失败");
        }
    }

    @GetMapping("/getBoardroomStatusByDate")
    public ResultVO getBoardroomStatusByDate(
            @RequestParam Integer id,
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            date = new Date(); // 默认为当天
        }

        List<BoardroomStatusDTO> statusList = boardroomService.getStatusByDate(id, date);
        return ResultVO.success("查询成功", statusList);
    }

    //Hailin Gong end
}
